package com.faxsun.api.wrapper.order;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

import org.broadleafcommerce.common.media.domain.Media;
import org.broadleafcommerce.core.catalog.dao.SkuDao;
import org.broadleafcommerce.core.catalog.domain.Sku;
import org.broadleafcommerce.core.order.domain.OrderItem;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.faxsun.core.catalog.domain.FSSkuImpl;
import com.faxsun.core.order.domain.FSOrderItemImpl;
import com.faxsun.core.order.service.type.FSInternalOrderStatus;

@Component("fsOrderItemWrapper")
@Scope("prototype")
@XmlRootElement(name = "fsOderItem")
@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlType(name="fsOrderItem")
public class OrderItemWrapper extends org.broadleafcommerce.core.web.api.wrapper.OrderItemWrapper {
	@XmlTransient //We don't want to serialize this so we make it XMLTransient
	@Resource(name="blSkuDao")
	private SkuDao skuDao;
	
	@XmlElement
	protected String urlOfSkuImage;
	
	@XmlElement
	protected String shippmentFee;
	
	@XmlElement
	protected String orderItemStatus;
	
	public void wrapDetails(OrderItem model, HttpServletRequest request){
		super.wrapDetails(model, request);
		this.urlOfSkuImage = getUrl(this.skuId);
		FSInternalOrderStatus status = ((FSOrderItemImpl)model).getStatus();
		if(status != null){
			orderItemStatus = status.getType();
		}
		shippmentFee =( (FSSkuImpl)skuDao.readSkuById(skuId)).getCalculatedShippmentFee().toString();
	}
	
	private String getUrl(Long id){
		String url = null;
		Sku sku = skuDao.readSkuById(id);
	    assert sku != null;
	    Media media = sku.getSkuMedia().get("primary");
	    if(media != null){
	    	url = media.getUrl();	
	    }
		return url;
	}

}
